package model

import (
	"errors"
	"fmt"
	"gitee.com/carollia/data-sdk-go/utils"
)

var deviceTypeList = []string{"imei", "idfa", "android_id", "oaid", "mac"}

// 设备行为事件
type Device struct {
	Type       string                 `json:"type"`
	Time       int64                  `json:"time"`
	Event      string                 `json:"event"`
	DeviceNo   string                 `json:"device_no"`
	DeviceType string                 `json:"device_type"`
	DeviceName string                 `json:"device_name"`
	Timezone   string                 `json:"timezone"`
	Properties map[string]interface{} `json:"properties"`
}

func (d *Device) Normalize() error {
	d.Type = "device"
	d.Time = utils.GetCNTimeNow()

	if d.DeviceNo == "" || len(d.DeviceNo) == 0 {
		return errors.New("property [DeviceNo] can't be empty")
	}
	if len(d.DeviceNo) > 255 {
		return errors.New("the max length of property [DeviceNo] is 255")
	}
	if !CheckDeviceType(d.DeviceType) {
		return errors.New(fmt.Sprintf("property [DeviceType] is not in %s", deviceTypeList))
	}

	if d.DeviceName == "" {
		return errors.New("property [DeviceName] can't be empty")
	}

	if d.Properties == nil {
		d.Properties = map[string]interface{}{}
	}
	return nil
}

func CheckDeviceType(deviceType string) (flag bool) {
	flag = false
	for _, v := range deviceTypeList {
		if v == deviceType {
			flag = true
			return
		}
	}
	return
}
